﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using CoreShop.Library;
using System.Data;
using System.Drawing.Printing;
using CoreShop.App;

namespace CoreShop
{
   /// <summary> 
    /// 客显  
    /// </summary>  
    public class CustomerDisplay
    {
        //public static SerialPort serialPort = new SerialPort();
        public static SerialPort serialPort = null;
        public static int mLedType = 0;
        public static void SetLedPara()
        {
            mLedType = CommFunc.ConvertToInt32(IniFiles.ReadIniData("CustShow", "LedType"));
            switch (mLedType)
            {
                case (int)GlobalEnumVars.LedCustShow.Common:
                    CustomerDisplay.OpenPort();
                    break;
                case (int)GlobalEnumVars.LedCustShow.VTOP:
                    VTopLed8c.OpenPort();
                    break;
            }
            
        }

        public static void OpenPort()
        {
            try
            {
                if (serialPort == null)
                { 
                    serialPort = new SerialPort();
                }

                if (!serialPort.IsOpen)
                {
                    serialPort.PortName = CommFunc.ConvertStrValue(IniFiles.ReadIniData("CustShow", "LedPort"));
                    serialPort.BaudRate = int.Parse(CommFunc.ConvertStrValue(IniFiles.ReadIniData("CustShow", "LedBaud")));
                    serialPort.StopBits = StopBits.One;
                    serialPort.DataBits = 8;
                    serialPort.Open();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("顾显出错：" + ex.Message);
            }
        
        }

        /// <summary>
        /// 顾显的调用
        /// </summary>
        /// <param name="strMoney"></param>
        /// <param name="iShowType"></param>
        public static void SetLedShow(string strMoney, int iShowType)
        {
            switch (mLedType)
            {
                case (int)GlobalEnumVars.LedCustShow.Common:
                    if (serialPort == null) return;
                    DisplayData(strMoney, iShowType);
                    break;
                case (int)GlobalEnumVars.LedCustShow.VTOP:
                    VTopLed8c.DisplayData(strMoney, iShowType);
                    break;
            }
        }

        /// <summary>  
        /// 数据信息展现  
        /// </summary>  
        /// <param name="data">发送的数据（清屏可以为null或者空）</param>  
        public static void DisplayData(string data, int DispiayType)
        {          
            try          
            {
                serialPort.DiscardInBuffer();
                //先清屏  
                //serialPort.WriteLine(((char)12).ToString());
                //指示灯(char)27=ESC  (char)115=s
                string str = ((char)27).ToString() + ((char)115).ToString() + ((int)DispiayType).ToString();
                serialPort.Write(str);

                //发送数据
                if (!string.IsNullOrEmpty(data))
                {
                    //(char)81=Q   (char)65=A   (char)13=CR
                    str = ((char)27).ToString() + ((char)81).ToString() + ((char)65).ToString() + data.ToString() + ((char)13).ToString();
                    serialPort.Write(str);
                }
            }
           catch (Exception ex)
           {
               MessageBox.Show("顾显出错：" + ex.Message);
           }
        }

        /// <summary>
        /// 开钱箱
        /// </summary>
        public static void OpenCashbox()
        {
            if (AppConstVars.mCashBoxType == (int)GlobalEnumVars.CashBoxType.UnUsed) return;
            if (AppConstVars.mCashBoxType == (int)GlobalEnumVars.CashBoxType.Drive)
            {
                PrintDocument pdc = new PrintDocument();
                //string printerName = pdc.PrinterSettings.PrinterName;      
                string printerName = pdc.PrinterSettings.PrinterName;
                //
                // string send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(255);   
                //标准     
                string send = "" + (char)(27) + (char)(112) + (char)(0) + (char)(60) + (char)(255);//非标准        
                RawPrinterHelper.SendStringToPrinter(printerName, send);
            }
            else
            {
                Units.Print p = new Units.Print(AppConstVars.mCashBoxPort);//:LPT1,LPT2...COM1,COM2...
                //p.PrintLine((char)27 + "p" + (char)0 + (char)60 + (char)255);   //开钱箱  
                p.PrintLine("" + (char)(27) + (char)(112) + (char)(0) + (char)(60) + (char)(255));   //开钱箱
            }
        }


    }

}

